/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/validate-binary-search-tree
   @Language: C++
   @Datetime: 19-07-10 19:57
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
	TreeNode *last=NULL;
public:
	bool isValidBST(TreeNode* root) {
		if(root==NULL) return true;
		if(!isValidBST(root->left)) return false;
		if(last && last!=root && last->val>=root->val) return false;
		last=root;
		if(!isValidBST(root->right)) return false;
		return true;
	}
};
